package com.cskaoyan.catmall.service.Impl;

import com.cskaoyan.catmall.dao.MarketCouponMapper;
import com.cskaoyan.catmall.dao.MarketCouponUserMapper;
import com.cskaoyan.catmall.dao.PromotionMapper;
import com.cskaoyan.catmall.model.bo.PromotionCouponBo;
import com.cskaoyan.catmall.model.pojo.MarketCoupon;
import com.cskaoyan.catmall.model.pojo.MarketCouponUser;
import com.cskaoyan.catmall.model.pojo.MarketCouponUserExample;
import com.cskaoyan.catmall.model.vo.ItemOfCouponListVo;
import com.cskaoyan.catmall.model.vo.PromotionBaseVo;
import com.cskaoyan.catmall.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 创建日期: 2022-01-09-20-13
 * author : LL
 * description:
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    PromotionMapper promotionMapper;

    @Autowired
    MarketCouponMapper couponMapper;
    @Autowired
    MarketCouponUserMapper couponUserMapper;

    @Override
    public PromotionBaseVo<ItemOfCouponListVo> getPromotionCoupons(PromotionCouponBo promotionCouponBo) {
        List<ItemOfCouponListVo> couponListVos = promotionMapper.getPromotionCoupons(promotionCouponBo);
        PromotionBaseVo<ItemOfCouponListVo> promotionBaseVo = new PromotionBaseVo<>();
        promotionBaseVo.setList(couponListVos);
        return promotionBaseVo;
    }





    @Override
    public void createCoupon(MarketCoupon marketCoupon) {
        marketCoupon.setAddTime(new Date());
        marketCoupon.setUpdateTime(new Date());
        couponMapper.insertSelective(marketCoupon);
        return;
    }

    @Override
    public MarketCoupon getPromotionCouponInfo(Integer id) {
        MarketCoupon marketCoupon = couponMapper.selectByPrimaryKey(id);
        return marketCoupon;
    }

    @Override
    public List<MarketCouponUser> getCouponUsers(MarketCouponUser couponUser) {
        MarketCouponUserExample couponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        criteria.andCouponIdEqualTo(couponUser.getCouponId());
        if (couponUser.getUserId() != null) {
            criteria.andUserIdEqualTo(couponUser.getUserId());
        }
        if (couponUser.getStatus() != null) {
            criteria.andStatusEqualTo(couponUser.getStatus());
        }
        List<MarketCouponUser> marketCouponUsers = couponUserMapper.selectByExample(couponUserExample);
        return marketCouponUsers;
    }

    @Override
    public void updateCoupon(MarketCoupon marketCoupon) {
        marketCoupon.setUpdateTime(new Date());
        couponMapper.updateByPrimaryKeySelective(marketCoupon);
    }

    @Override
    public void deleteCoupon(MarketCoupon marketCoupon) {
        marketCoupon.setDeleted(true);
        couponMapper.updateByPrimaryKey(marketCoupon);
    }
}